<?php

namespace TencentYun\Sms\SDK;

use TencentYun\Sms\SDK\SmsSingleSender;
use App\Core\Utils\TwHelper\CommFunction;
use App\Exceptions\Response\Service\ServiceCode;
use App\Exceptions\Response\Service\ServiceException;

class SmsService
{

    const APP_ID = 1400001627;
    const APP_KEY = 'aaff6c9e62fe88d0be4dd66f255e3c67';
    const SIGNATURE_CN = '【通心粉社区】';//中文签名
    const SIGNATURE_EN = '【TongXinFen Club】';

    /**
     * @param int $type 0:普通短信;1:营销短信（强调：要按需填值，不然会影响到业务的正常使用）
     * @param int $nationCode 国家码"86"中国
     * @param $mobile 手机号
     * @param $msg 内容
     *
     * 参考：https://cloud.tencent.com/document/product/382/9557
     * 返回结果：数组 ["result"]=> int(0) ["errmsg"]=> string(2) "OK"
     *             ["ext"]=> string(0) "" ["sid"]=> string(31) "1:15149618340621890081119539999" ["fee"]=> int(1)
     *  "result": 0, //0表示成功(计费依据)，非0表示失败
     *  "errmsg": "OK", //result非0时的具体错误信息
     */
    public static function sendMsg($phoneNumber, $msg, $type = 0, $nationCode)
    {
            $sender = new SmsSingleSender(self::APP_ID, self::APP_KEY);
            if ($nationCode == 86) {
                if(!CommFunction::checkMobile($phoneNumber)){
                    throw new ServiceException(ServiceCode::SERVICE_OAUTH_MOBILE_FORMAT_IS_WRONG, '', []);
                }
                $content = self::SIGNATURE_CN . $msg;
            } else {
                //英文还没有定,暂时使用中文签名
                $content = self::SIGNATURE_CN . $msg;
            }
            $result = $sender->send($type, $nationCode, $phoneNumber, $content, "", "");
            return get_object_vars(json_decode($result));
    }

}

